<?php
/**
 * Created by PhpStorm.
 * User: xsp
 * Date: 2018/4/15
 * Time: 19:40
 */

namespace frontend\controllers;


use backend\models\Product;
use yii\filters\AccessControl;


class IndexController extends CommonController
{

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',

            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }
    public function behaviors()
    {
        return [
            [
                'class' => 'yii\filters\AjaxFilter',//验证允许ajax执行的动作
                'only' => ['add'],
                'errorMessage'=>'Request must be XMLHttpRequest.'//当请求不是ajax请求是输出错误信息

            ],
//            'access' => [
//                'class' => AccessControl::className(),
//                'only' => ['*'],
//                'except'=>['login'],
//                'denyCallback'=>function($rule,$action)
//                {
//                    return $this->redirect(['product/add']);//如果拒绝访问，则执行该方法
//                },
//                'rules' => [
//                    [
//                        'allow' => false,
//                        'roles' => ['?'],
//                        'actions' => ['index'],
//                        'denyCallback'=>function($rule,$action)
//                        {
//                            return $this->redirect(['product/index']);
//                        }
//
//                    ],
//                    [
//                        'allow' => true,
//                        'actions' => ['index', 'check', 'Add', 'Confirm'],
//                        'roles' => ['@']
//                    ]
//                ]
//            ]
        ];
    }

    public function actionIndex()
    {
        $this->layout = 'layout1';
        $data['tui'] = Product::find()->where('istui = "1" and ison = "1"')->orderby('createtime desc')->limit(4)->all();
        $data['new'] = Product::find()->where('ison = "1"')->orderby('createtime desc')->limit(4)->all();
        $data['hot'] = Product::find()->where('ison = "1" and ishot = "1"')->orderby('createtime desc')->limit(4)->all();
        $data['all'] = Product::find()->where('ison = "1"')->orderby('createtime desc')->limit(7)->all();
        return $this->render("index", ['data' => $data]);
    }
}